package com.work.dyduser.module.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseFragment;
import com.work.dyduser.module.pagetwo.CustomRecordActivity;
import com.work.dyduser.module.pagetwo.VideoActivity;
import com.work.dyduser.utils.UIUtils;

/**
 * @author WZ 2016/12/3
 *         分类
 */
public class FenLeiFragment extends BaseFragment {

    private static final int CAMERA_PERMISSIONS_REQUEST_CODE_TWO = 11010;
    private ImageView video;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_paishe_fragment;
    }

    @Override
    protected void initView() {
        video = (ImageView) findViewById(R.id.iv_video);
    }

    @Override
    protected void initData() {
        super.initData();
        video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoObtainCameraPermission();
            }
        });
    }

    /**
     * 自动获取相机权限
     */
    private void autoObtainCameraPermission() {
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
                UIUtils.showToast("您已经拒绝过一次");
            }
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO}, CAMERA_PERMISSIONS_REQUEST_CODE_TWO);
        } else {//有权限直接调用系统相机拍照
            openVideo();
        }
    }

    //打开拍摄页面
    private void openVideo() {
        //去拍摄
        Intent intent = new Intent(getContext(), VideoActivity.class);
//        Intent intent = new Intent(getContext(), CustomRecordActivity.class);
        UIUtils.startActivity(intent);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case CAMERA_PERMISSIONS_REQUEST_CODE_TWO://调用系统相机申请拍照权限回调
                openVideo();
                break;

        }
    }

}
